package com.etrans.web.gps.business.systemProcessing.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.orderReceiving.domain.Alarm;
import com.etrans.web.gps.business.systemProcessing.controller.SystemProcessingQuartzJob;
import com.etrans.web.gps.business.systemProcessing.domain.SystemProcessing;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.common.message.MailSendUtil;
import com.etrans.web.gps.common.message.SMSUtil;
import com.etrans.web.gps.util.QuartzManager;

@Service
public class SystemProcessingService extends BaseService{
	
	/**
	 * 列表
	 * @param paramsMap
	 * @return
	 */
	public List<SystemProcessing> queryAllSystemProcessing(HashMap<String, Object> paramsMap) {
		List<SystemProcessing> list = selectList("systemProcessingQuery.queryAllSystemProcessing", paramsMap);
		return list;
	}
	
	public List<SystemProcessing> querySystemProcessingByEnable(HashMap<String, Object> paramsMap) {
		List<SystemProcessing> list = selectList("systemProcessingQuery.querySystemProcessingByEnable", paramsMap);
		return list;
	}
	
	public Integer queryAllSystemProcessingCount(HashMap<String, Object> paramMap) {
		Integer total=(Integer) this.selectOne("systemProcessingQuery.queryAllSystemProcessingCount", paramMap);
		return total;
	}
	
	/**
	 * 根据参数查询数量
	 * @param paramMap
	 * @return
	 */
	public Integer querySystemProcessingCount(HashMap<String, Object> paramMap) {
		Integer count=(Integer) this.selectOne("systemProcessingQuery.querySystemProcessingCount", paramMap);
		return count;
	}
	
	/**
	 * 报警类型列表
	 * @param paramsMap
	 * @return
	 */
	public List<HashMap<String, Object>> queryAllAlarmType() {
		List<HashMap<String, Object>> list = selectList("systemProcessingQuery.queryAllAlarmType", null);
		return list;
	}

	public boolean saveHandleDetail(Map<String, Object> map) {
		int i =this.insert("systemProcessingMapper.saveSystemProcessing", map);
		return i==1?true:false;
	}
	
	public boolean deleteStemProcessingByID(Map<String, Object> map) {
		String id = map.get("id").toString();
		//删除任务调度
		QuartzManager.removeJob(id, id, id, id); 
		int i =this.delete("systemProcessingMapper.deleteStemProcessingByID", map);
		return i==1?true:false;
	}
	
	public boolean updateStemProcessingByID(Map<String, Object> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String id = map.get("id").toString();
		long notice_time = Long.parseLong((String) map.get("notice_time"));
		String[] time = sdf.format(new Date(notice_time)).split(":");
		String cron=time[2]+" "+time[1]+" "+time[0]+" * * ?";
		try {  
			QuartzManager.removeJob(id, id, id, id); 
            System.out.println("【启用成功】通知时间："+sdf.format(new Date(notice_time))+".....");    
            QuartzManager.addJob(id, id, id, id, SystemProcessingQuartzJob.class, cron);    
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
		int i =this.update("systemProcessingMapper.updateStemProcessingByID", map);
		return i==1?true:false;
	}
	
	private  List<String> queryEmailOrPhone(Alarm alarm,Integer notice_obj, String type){
		Map<String,Object> map= new HashMap<String, Object>();
		map.put("vehicle_no", alarm.getVehicle_no());
		map.put("vehicle_color", alarm.getVehicle_color());
		map.put("enterprise_id", alarm.getEnterprise_id());
		map.put("alarm_area", alarm.getArea_id());
		map.put("platform_id", alarm.getPlatform_id());
		List<String> Mblist = new ArrayList<String>();
		List<String> Emlist = new ArrayList<String>();
		List<Map<String,Object>> vlist = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> elist = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> plist = new ArrayList<Map<String,Object>>();
		List<Map<String,Object>> alist = new ArrayList<Map<String,Object>>();
		if(notice_obj==1){
			vlist = this.selectList("systemProcessingQuery.queryDriver",map);
		}
		if(notice_obj==3){
			elist = this.selectList("systemProcessingQuery.queryEnterprise",map);
		}
		if(notice_obj==4){
			alist = this.selectList("systemProcessingQuery.queryOrganize",map);
		}
		if(notice_obj==5){
			plist = this.selectList("systemProcessingQuery.queryPlatform",map);
		}
		if(elist!=null){
			for (Map<String, Object> map0 : elist) {
				Emlist.add((String)map0.get("email"));
				Mblist.add((String)map0.get("phone"));
			}
		}
		if(plist!=null){
			for (Map<String, Object> map1 : plist) {
				Emlist.add((String)map1.get("email"));
				Mblist.add((String)map1.get("phone"));
			}		
		}
		if(alist!=null){
			for (Map<String, Object> map2 : alist) {
				Emlist.add((String)map2.get("email"));
				Mblist.add((String)map2.get("phone"));
			}
		}
		if(vlist!=null){
			for (Map<String, Object> map3 : vlist) {
				Mblist.add((String)map3.get("phone"));
			}
		}
		return "phone".equals(type)?Mblist:Emlist;
	}
	
	
	
	public String sendInform(Alarm alarm, SystemProcessing vo){
		String tips = null;
		Integer type = vo.getNotice_type();
		List<String> list = null;
		String msg = vo.getHandle_remark();
		if(type==1){
			Map<String,Object> map= new HashMap<String, Object>();
			Integer DriverId = this.getDriverId(alarm.getVehicle_no(),alarm.getVehicle_color());
			if(DriverId!=null){
				map.put("driverId",DriverId);
				map.put("message",msg);
				this.insert("systemProcessingMapper.saveAppInform",map);
				tips="success";
			}else{
				tips="处理失败，当前司机未注册APP账号！";
			}
		}else if(type==2){
			list =  queryEmailOrPhone(alarm,vo.getNotice_obj(),"phone");
			if(list!=null&&list.size()>=0){
				for (String phone : list) {
					SMSUtil.sendMessage(phone,"【车辆联网联控平台】"+msg,"","","");
				}
				tips="success";
			}else{
				tips="处理失败，无法获取手机号码！";
			}

		}else if(type==3){
			list =  queryEmailOrPhone(alarm,vo.getNotice_obj(),"Email");
			if(list!=null&&list.size()>=0){
				for (String email : list) {
					MailSendUtil.sendEmail(email, "报警处理提醒",email, "【车辆联网联控平台通知】"+msg);
				}
				tips="success";
			}else{
				tips="处理失败，无法找到邮箱地址！";
			}
		}
		return tips;
	}
	
	public void saveDetail(Alarm alarm, SystemProcessing vo) {
		Map<String,Object> map= new HashMap<String, Object>();
		Map<String,Object> param= new HashMap<String, Object>();
		param.put("enterprise_id", alarm.getEnterprise_id());
		param.put("platform_id", alarm.getPlatform_id());
		List<Map<String,Object>> elist  = this.selectList("systemProcessingQuery.queryEnterprise",param);
		List<Map<String,Object>> plist  = this.selectList("systemProcessingQuery.queryPlatform",param);
		map.put("vehicle_no", alarm.getVehicle_no());
		map.put("vehicle_color", alarm.getVehicle_color());
		map.put("alarm_type", alarm.getAlarm_type());
		map.put("alarm_src", alarm.getAlarm_src());
		map.put("alarm_time", alarm.getAlarm_time());
		map.put("alarm_area", alarm.getArea_id());
		map.put("handle_type", 1);
		map.put("handle_name", "admin");
		map.put("handle_remark", vo.getHandle_remark());
		map.put("notice_obj", vo.getNotice_obj());
		map.put("notice_type", vo.getNotice_type());
		map.put("platform_id", alarm.getPlatform_id());
		map.put("enterprise_id", alarm.getEnterprise_id());
		for (Map<String, Object> map1 : elist) {
			map.put("enterprise_name", map1.get("name"));
		}
		for (Map<String, Object> map2 : plist) {
			map.put("platform_name", map2.get("pname"));
		}
		this.insert("orderReceivingMapper.saveHandleDetail", map);
	}
	
	public Integer getDriverId(String vehicle_no,Integer vehicle_color){
		HashMap<String,Object> whereMap = new  HashMap<String,Object>();
		whereMap.put("vehicle_color",vehicle_color);
		whereMap.put("vehicle_no",vehicle_no);
		String licence = null;
		List<Document> list = MGTemplete.find("egov", "driver_vehicle_swap",whereMap);
		for (Document document : list) {
			licence=(String)document.get("licence");
		}
		whereMap.put("licence",licence);
		Integer DriverId =this.selectOne("systemProcessingQuery.queryDriverId",whereMap);
		return DriverId;
	} 
}
